package com.infitecs.eyas.boiler.service.impl;

import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.dao.SodaParameterDao;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.model.SodaParameter;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.boiler.service.SodaParameterService;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.enthalpy.service.EnthalpyService;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.mill.dao.MillSelectDao;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerDao;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.runcoalbin.service.CoalBunkerService;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.eyas.util.Parameter;
import com.infitecs.eyas.util.ParametersUtil;
import fr.expression4j.core.Expression;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.core.impl.ExpressionImpl;
import fr.expression4j.factory.ExpressionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: chenkaiyu
 * Date: 15-5-26
 * Time: 下午4:49
 * To change this template use File | Settings | File Templates.
 */
@Service("SodaParameterService")
@Transactional
public class SodaParameterServiceImpl implements SodaParameterService {
    @Autowired
    private SodaParameterDao sodaParameterDao;
    @Autowired
    private ExpressionDao expressionDao;
    @Autowired
    private EnthalpyService enthalpyService;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private EfficiencyService efficiencyService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private CoalBunkerDao coalBunkerDao;
    @Autowired
    private CoalBunkerService coalBunkerService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private BurnDao burnDao;
    @Autowired
    private CleanService cleanService;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private ChimneyService chimneyService;
    @Autowired
    private MillSelectDao millSelectDao;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private PipeCalculateService pipeCalculateService;
    @Autowired
    private PipeCalculateDao pipeCalculateDao;
    @Override
    public void saveTheValueOfE3(double E3, int powerCaseId) {
        sodaParameterDao.updateE3(E3, powerCaseId);
    }

    @Override
    public void saveTheValueOfE4(double E4, int powerCaseId) {
        sodaParameterDao.updateE4(E4, powerCaseId);
    }

    @Override
    public void saveTheValueOfE5(double E5, int powerCaseId) {
        sodaParameterDao.updateE5(E5, powerCaseId);
    }

    @Override
    public void saveTheValueOfE6(double E6, int powerCaseId) {
        sodaParameterDao.updateE6(E6, powerCaseId);
    }

    @Override
    public void saveTheValueOfE7(double E7, int powerCaseId) {
        sodaParameterDao.updateE7(E7, powerCaseId);
    }

    @Override
    public void saveTheValueOfE8(double E8, int powerCaseId) {
        sodaParameterDao.updateE8(E8, powerCaseId);
    }

    @Override
    public void saveTheValueOfE9(double E9, int powerCaseId) {
        sodaParameterDao.updateE9(E9, powerCaseId);
    }

    @Override
    public void saveTheValueOfE13(double E13, int powerCaseId) {
        sodaParameterDao.updateE13(E13, powerCaseId);
    }

    @Override
    public void saveTheValueOfE14(double E14, int powerCaseId) {
        sodaParameterDao.updateE14(E14, powerCaseId);
    }

    @Override
    public void saveTheValueOfE15(double E15, int powerCaseId) {
        sodaParameterDao.updateE15(E15, powerCaseId);
    }

    @Override
    public void saveTheValueOfE16(double E16, int powerCaseId) {
        sodaParameterDao.updateE16(E16, powerCaseId);
    }

    @Override
    public void saveTheValueOfE17(double E17, int powerCaseId) {
        sodaParameterDao.updateE17(E17, powerCaseId);
    }

    @Override
    public void saveTheValueOfE18(double E18, int powerCaseId) {
        sodaParameterDao.updateE18(E18, powerCaseId);
    }

    @Override
    public void saveTheValueOfE19(double E19, int powerCaseId) {
        sodaParameterDao.updateE19(E19, powerCaseId);
    }

    @Override
    public void saveTheValueOfE10(double E10, int powerCaseId) {
        sodaParameterDao.updateE10(E10, powerCaseId);
    }

    @Override
    public void saveTheValueOfE11(double E11, int powerCaseId) {
        sodaParameterDao.updateE11(E11, powerCaseId);
    }

    @Override
    public void saveTheValueOfE12(double E12, int powerCaseId) {
        sodaParameterDao.updateE12(E12, powerCaseId);
    }

    @Override
    public void saveTheValueOfE20(double E20, int powerCaseId) {
        sodaParameterDao.updateE20(E20, powerCaseId);
    }

    @Override
    public void saveTheValueOfE21(double E21, int powerCaseId) {
        sodaParameterDao.updateE21(E21, powerCaseId);
    }

    @Override
    public void saveTheValueOfE22(double E22, int powerCaseId) {
        sodaParameterDao.updateE22(E22, powerCaseId);
    }

    @Override
    public void saveTheValueOfE23(double E23, int powerCaseId) {
        sodaParameterDao.updateE23(E23, powerCaseId);
    }

    @Override
    public void saveTheValueOfE24(double E24, int powerCaseId) {
        sodaParameterDao.updateE24(E24, powerCaseId);
    }

    @Override
    public void saveTheValueOfE25(double E25, int powerCaseId) {
        sodaParameterDao.updateE25(E25, powerCaseId);
    }

    @Override
    public void saveTheValueOfE26(double E26, int powerCaseId) {
        sodaParameterDao.updateE26(E26, powerCaseId);
    }

    @Override
    public void saveTheValueOfE27(double E27, int powerCaseId) {
        sodaParameterDao.updateE27(E27, powerCaseId);
    }

    @Override
    public void saveTheValueOfE29(double E29, int powerCaseId) {
        sodaParameterDao.updateE29(E29, powerCaseId);
    }

    @Override
    public void saveTheValueOfE30(double E30, int powerCaseId) {
        sodaParameterDao.updateE30(E30, powerCaseId);
    }

    @Override
    public SodaParameter getSodaParameter(int caseId) {
        return sodaParameterDao.getSodaParameter(caseId);
    }

    @Override
    public void insertCaseId(int caseId) {
        sodaParameterDao.insertCaseId(caseId);
    }

    @Override
    public double calculateE30(double E3, double E29, int caseId) {
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_BOILER_1);
        double E30 = 0;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("E3", E3));
            params.add(new Parameter("E29", E29));
            E30 = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        } finally {
            saveTheValueOfE30(E30, caseId);
        }
        return E30;
    }

    @Override
    public void sodaParameterSave(SodaParameter sodaParameter) {
        sodaParameterSaveData(sodaParameter);
        //更新锅炉效率及燃煤量
        Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(sodaParameter.getPowerCaseId());
        if (oldEfficiency == null) return;
        OperateField operateField = operateFieldDao.getOperateField(sodaParameter.getPowerCaseId());
        oldEfficiency.setE52State(operateField.getE52State());
        oldEfficiency.setE89State(operateField.getE89State());
        oldEfficiency.setE90State(operateField.getE90State());
        oldEfficiency.setE92State(operateField.getE92State());
        oldEfficiency.setE93State(operateField.getE93State());
        oldEfficiency.setE94State(operateField.getE94State());
        oldEfficiency.setE95State(operateField.getE95State());
        efficiencyService.efficiencySaveData(oldEfficiency);
        //更新原煤仓
        CoalBunker oldCoalBunker = coalBunkerDao.getCoalBunker(sodaParameter.getPowerCaseId());
        //更新原煤仓信息
        if (oldCoalBunker != null) {
            oldCoalBunker.setE9State(operateField.getE9State());
            coalBunkerService.designSaveData(oldCoalBunker);
        }
        //更新燃烧
        CalculateBurn calculateBurn = burnDao.getCalculateBurnByCaseId(sodaParameter.getPowerCaseId());
        if (calculateBurn != null) {
            calculateBurn.setYyglState(operateField.getYyglState());
            calculateBurn.setE54State(operateField.getE54State());
            calculateBurn.setE88State(operateField.getE88State());
            calculateBurn.setE122State(operateField.getE122State());
            calculateBurn.setE155State(operateField.getE155State());
            calculateBurn.setE171State(operateField.getE171State());
            burnService.burnSaveData(calculateBurn);
        }
        //更新烟气净化系统
        CalculateClean calculateClean = cleanDao.getCalculateClean(sodaParameter.getPowerCaseId());
        if (calculateClean != null) {
            calculateClean.setE221State(operateField.getE221State());
            calculateClean.setE222State(operateField.getE222State());
            calculateClean.setE231State(operateField.getE231State());
            calculateClean.setE241State(operateField.getE241State());
            calculateClean.setE242State(operateField.getE242State());
            calculateClean.setE248State(operateField.getE248State());
            calculateClean.setE258State(operateField.getE258State());
            cleanService.cleanDataSave(calculateClean);
        }
        //更新烟囱出口直径计算
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(sodaParameter.getPowerCaseId());
        if (calculateChimney != null) {
            calculateChimney.setE314State(operateField.getE314State());
            calculateChimney.setE315State(operateField.getE315State());
            calculateChimney.setE316State(operateField.getE316State());
            calculateChimney.setCaseId(operateField.getCaseId());
            chimneyService.chimneySaveData(calculateChimney);
        }

                //更新磨煤机选择
        MillSelect millSelect = millSelectDao.getMillSelect(sodaParameter.getPowerCaseId());
        if (millSelect != null) {
            millSelect.setMyglState(operateField.getMyglState());
            millSelect.setE12State(operateField.getE12State());
            millSelect.setE74State(operateField.getE74State());
            millSelect.setE75State(operateField.getE75State());
            millSelect.setModelState(operateField.getModelState());
            millSelectService.chooseDataSave(millSelect);
        }
        //更新煤粉管道计算
        PipeCalculate pipeCalculate = pipeCalculateDao.getPipeCalculate(sodaParameter.getPowerCaseId());
        if (pipeCalculate != null) {
            pipeCalculateService.pipelineDataSave(pipeCalculate);
        }
    }

    private void sodaParameterSaveData(SodaParameter sodaParameter) {
        //计算过热蒸汽比焓igr
        sodaParameter.setIgr(calculateE6(sodaParameter));
        //给水比焓igs
        sodaParameter.setIgs(calculateE9(sodaParameter));
        //汽包压力下饱和水比焓iqb
        sodaParameter.setIqb(calculateE12(sodaParameter));
        //计算过热蒸汽比焓izr
        sodaParameter.setIzr(calculateE16(sodaParameter));
        //计算再热蒸汽入口比焓izr³
        sodaParameter.setIzr3(calculateE19(sodaParameter));
        //计算过热器减温水比焓hdes.S
        sodaParameter.setHdesS(calculateE23(sodaParameter));
        //计算再热器减温水比焓hdes.R
        sodaParameter.setHdesR(calculateE27(sodaParameter));
        //连续排污量DBw
        sodaParameter.setDbw(calculateE30(sodaParameter));
        //校验新增或者删除
        SodaParameter oldData = sodaParameterDao.getSodaParameter(sodaParameter.getPowerCaseId());
        if (oldData == null) {
            sodaParameterDao.addSodaParameter(sodaParameter);
        } else {
            sodaParameterDao.updateSodaParameter(sodaParameter);
        }

    }

    public double calculateE6(SodaParameter sodaParameter) {
        double E4 = sodaParameter.getPgr();
        double E5 = sodaParameter.getTgr();
        if (E4 == 0 && E5 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E4, E5 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE9(SodaParameter sodaParameter) {
        double E7 = sodaParameter.getPgs();
        double E8 = sodaParameter.getTgs();
        if (E7 == 0 && E8 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E7, E8 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE12(SodaParameter sodaParameter) {
        double E10 = sodaParameter.getPqb();
        double E11 = sodaParameter.getTqb();
        if (E10 == 0 && E11 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E10, E11 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE16(SodaParameter sodaParameter) {
        double E14 = sodaParameter.getPzr();
        double E15 = sodaParameter.getTzr();
        if (E14 == 0 && E15 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E14, E15 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE19(SodaParameter sodaParameter) {
        double E17 = sodaParameter.getPzr3();
        double E18 = sodaParameter.getTzr3();
        if (E17 == 0 && E18 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E17, E18 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE23(SodaParameter sodaParameter) {
        double E21 = sodaParameter.getPdesS();
        double E22 = sodaParameter.getTdesS();
        if (E21 == 0 && E22 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E21, E22 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE27(SodaParameter sodaParameter) {
        double E25 = sodaParameter.getPdesR();
        double E26 = sodaParameter.getTdesR();
        if (E25 == 0 && E26 == 0) {
            return 0;
        }
        try {
            return DoubleUtil.getInstance().get3Double(enthalpyService.culB6(E25, E26 + Constant.KELVIN_TEMPERATURE));
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    public double calculateE30(SodaParameter sodaParameter) {
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_BOILER_1);
        double E30 = 0;
        double E3 = sodaParameter.getDgr();
        double E29 = sodaParameter.getDpw();
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("E3", E3));
            params.add(new Parameter("E29", E29));
            E30 = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return E30;
    }
}
